package com.gljx.util;

import com.gljx.web.Entity.Student;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;

import java.io.*;

public class ReadTxt {

    public static Student readTxtInformation(String path) {
        Student student = new Student();
        try { // 防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw

            /* 读入TXT文件 */
            String pathname = path+"\\wz.txt"; // 绝对路径或相对路径都可以，这里是绝对路径，写入文件时演示相对路径
            File filename = new File(pathname); // 要读取以上路径的input。txt文件
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(filename), "gbk"); // 建立一个输入流对象reader
            BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
            String line = null;
            String rows = "";
            line = br.readLine();
            while (line != null) {
                rows += line + ",";
                line = br.readLine(); // 一次读入一行数据
            }
            String[] datas = rows.split(",");
            student.setFnameL2(datas[0]);
            if (datas[1].equals("女")) {
                student.setCfsex((long) 2);
            } else {
                student.setCfsex((long) 1);
            }

            student.setCfnation(datas[2]);
            student.setCfbirthdays(datas[3]);
            student.setCfaddress(datas[4]);
            student.setCfidcardno(datas[5]);

            /* 写入Txt文件 */
           /* File writename = new File(".\\result\\en\\output.txt"); // 相对路径，如果没有则要建立一个新的output。txt文件
            writename.createNewFile(); // 创建新文件
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write("我会写入文件啦\r\n"); // \r\n即为换行
            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后记得关闭文件*/

        } catch (Exception e) {
            e.printStackTrace();
        }
        return student;
    }



    public  static int CVR_InitComm(int Port,String path) throws NativeException, IllegalAccessException, UnsupportedEncodingException
    {
        JNative n = null;
        try
        {
            n = new JNative(path+"\\Termb.dll", "CVR_InitComm");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Port);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        }
        finally
        {

        }
    }



    public static  int CVR_Authenticate(String path) throws NativeException, IllegalAccessException
    {
        JNative n = null;
        try
        {
            n = new JNative(path+"\\Termb.dll", "CVR_Authenticate");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        }
        finally
        {

        }
    }

    public static  int CVR_Read_Content(int Active,String path) throws NativeException, IllegalAccessException
    {
        JNative n = null;
        try
        {
            n = new JNative(path+"\\Termb.dll", "CVR_Read_Content");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Active);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        }
        finally
        {

        }
    }

    private  int CVR_CloseComm() throws NativeException,IllegalAccessException
    {
        JNative n = null;
        try
        {
            n = new JNative("E:\\DrivingSys\\src\\com\\gljx\\util\\Termb.dll", "CVR_CloseComm");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        }
        finally
        {

        }
    }

}